﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xproject.Framework.Core;
using Xproject.Framework.Core.DataContext;
using Xproject.Framework.Core.Repositories;
using Model.ZXBuiness;
using Repository.ZXBuiness;
using Repository.Util;
using Model.Util;

namespace Services.ZXBuiness
{
    public class UserFenRunServices : Service
    {
        IRepositoryAsync<ZXUserFenRun> m_ZXUserFenRunRepository;
        IRepositoryAsync<ZXTiXianApply> m_ZXTiXianApplyRepository;
        IRepositoryAsync<ZXOrderFenRunInfo> m_ZXOrderFenRunInfoRepository;
        IRepositoryAsync<Identity> m_IdentityRepository;

        public UserFenRunServices(IDataContextAsync dataContext) : base(dataContext)
        {
            m_ZXUserFenRunRepository = m_UnitOfWork.RepositoryAsync<ZXUserFenRun>();
            m_ZXTiXianApplyRepository = m_UnitOfWork.RepositoryAsync<ZXTiXianApply>();
            m_ZXOrderFenRunInfoRepository = m_UnitOfWork.RepositoryAsync<ZXOrderFenRunInfo>();
            m_IdentityRepository = m_UnitOfWork.RepositoryAsync<Identity>();
        }

        public ZXOrderFenRunInfo GetOrderFRinfo(long OrderFengyunId)
        {
            return null;
        }

        public ZXUserFenRun GetUserFenRun(long UserFenRunId)
        {
            return m_ZXUserFenRunRepository.GetById(UserFenRunId);             
        }

        public List<ZXUserFenRun> GetMyFenRunList(int? offset, int? limit, long zxuserId,int? state)
        {
            return m_ZXUserFenRunRepository.GetFenRunList(offset.Value, limit.Value, zxuserId,state.Value);
        }
        /// <summary>
        /// 0 失败 1正常 2 已存在
        /// </summary>
        /// <param name="UserFenRunId"></param>
        /// <param name="ZX_UserId"></param>
        /// <param name="OrderFengyunId"></param>
        /// <returns></returns>
        public int  AddZXTiXianApply(long UserFenRunId, long ZX_UserId,long OrderFengyunId,string userCode)
        {
            if (GetZXTiXianApplyByUFid(UserFenRunId) != null)
            {
                return 2;
            }
            ZXUserFenRun U = GetUserFenRun(UserFenRunId);
            ZXTiXianApply zx = new ZXTiXianApply();
            zx.TiXianId = Guid.NewGuid().ToString();
            zx.ApplyTime = DateTime.Now;
            zx.ApproveMoney = U.FenRunMoney;
            zx.ApplyUserCode = userCode;
            zx.OrderFengyunId = OrderFengyunId;
            zx.State = 0;
            zx.UserFenRunId = UserFenRunId;
            zx.ZX_UserId = ZX_UserId;
            zx.EntityState = EntityState.Added;
            m_ZXTiXianApplyRepository.Insert(zx);
            U.IsTiXian = 5;
            U.TiXianTime = DateTime.Now;
            U.FRcode = CreateFRCode("FR", UserFenRunId.ToString());
            U.EntityState = EntityState.Modified;
            m_ZXUserFenRunRepository.Update(U);
            int result = m_UnitOfWork.SaveChanges();
                if (result >=1)
                {
                    return 1;
                }
          

            return 0;
        }

        /// <summary>
        /// 创建分润申请业务码
        /// </summary>
        /// <param name="pre"></param>
        /// <param name="projectId"></param>
        /// <returns></returns>
        public string CreateFRCode(string pre, string projectId)
        {
            Identity iden = new Identity();
            iden.CreateTime = DateTime.Now;
            iden.EntityState = EntityState.Added;
            iden.ProjectID = projectId;
            iden.Guid = Guid.NewGuid().ToString();
            m_IdentityRepository.Insert(iden);
            m_UnitOfWork.SaveChanges();
            Identity nid = m_IdentityRepository.Get(iden.Guid);
            string year = DateTime.Now.Year.ToString().Substring(2, 2);
            string Monty = DateTime.Now.Month.ToString();
            string day = DateTime.Now.Day.ToString();

            return pre + year + Monty + day + nid.ID.ToString();
        }

        public ZXTiXianApply GetZXTiXianApplyByUFid(long UserFenRunId)
        {
            return m_ZXTiXianApplyRepository.GetByUFId(UserFenRunId);
        }

        public ZXFenRunDetailModel GetZXFenRunDetail(long UserFenRunId)
        {
            return m_ZXUserFenRunRepository.GetFenRunOrderInfoByUFRid(UserFenRunId);
        }

    }
}
